package com.kadron.order;

import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

import com.kadron.common.KConst;
import com.kadron.creator.KOrderModelCreator;
import com.kadron.dao.KImageDAO;
import com.kadron.dao.KOrderDAO;
import com.kadron.dao.KOrderItemDAO;
import com.kadron.dao.KUserDAO;
import com.kadron.dao.KUserImageDAO;
import com.kadron.datamodel.KDataModel;
import com.kadron.datamodel.KOrderModelDataProvider;
import com.kadron.entity.User;
import com.kadron.entity.UserImage;
import com.kadron.manager.KMessageManager;
import com.kadron.manager.KSelection;

@AutoCreate
@Scope(ScopeType.PAGE)
@Name(KUserOrder.K_NAME)
public class KUserOrder extends KMessageManager {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2240644824574701293L;
	
	public static final String K_NAME = "kUserOrder";

	private KDataModel orderList;
	private KOrderModelDataProvider dataProvider;
	
	private User user; 
	private UserImage img;
	
	@In
	private KOrderDAO orderDAO;
	@In
	private KOrderItemDAO orderItemDAO;
	@In
	private KImageDAO imageDAO;
	@In
	private KUserImageDAO userImageDAO;
	@In
	private KUserDAO userDAO;
	
	@Create
	public void init() {
		Long userId = (Long) Component.getInstance("userId");
		if (userId == null || userId <= 0) {
			setError(true);
			return;
		}
		user = (User) userDAO.find(userId, User.class);
		img = userImageDAO.findByUser(userId);
		dataProvider = new KOrderModelDataProvider(user.getId(),new KOrderModelCreator(orderDAO, orderItemDAO, imageDAO), orderDAO);
		orderList = new KDataModel(dataProvider);
	}

	public KDataModel getOrderList() {
		return orderList;
	}
	
	public String back() {
		if (user == null) {
			bad(KConst.BAD_THING);
			return null;
		}
		KSelection kselection = (KSelection) Component.getInstance("kSelection");
		kselection.setItemId(user.getId());
		return "e_user";
	}
	
	public User getUser() {
		return user;
	}

	public UserImage getImg() {
		return img;
	}
}
